package com.spring.gowhere;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.spring.framgent.Index_Fragment;
import com.spring.util.DummyTabContent;

public class HotelTab extends FragmentActivity{

	TabHost tabHost;
	TabWidget tabWidget; 

	int CURRENT_TAB = 0;	//设置常量

	Index_Fragment index_Fragment;
	android.support.v4.app.FragmentTransaction ft;
	LinearLayout tabIndicator1,tabIndicator2,tabIndicator3,tabIndicator4,tabIndicator5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        WindowManager.LayoutParams  layoutParams		=getWindow().getAttributes();
        layoutParams.gravity			=Gravity.BOTTOM;
        layoutParams.width				=WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height				=WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
        
        
        findTabView();
        tabHost.setup();
        /** 监听*/
        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener(){
			@Override
			public void onTabChanged(String tabId) {
				
				/**碎片管理*/
				android.support.v4.app.FragmentManager fm =  getSupportFragmentManager();


				index_Fragment	=(Index_Fragment) fm.findFragmentByTag("index");
				ft = fm.beginTransaction();




				
				if(index_Fragment!=null)
					ft.hide(index_Fragment);
				
				/** 如果当前选项卡是home */
				if(tabId.equalsIgnoreCase("home")){
					isTabHome();
					CURRENT_TAB = 1;
					
				/** 如果当前选项卡是wall */
				}else if(tabId.equalsIgnoreCase("wall")){	
					isTabWall();
					CURRENT_TAB = 2;
					
				/** 如果当前选项卡是message */
				}else if(tabId.equalsIgnoreCase("index")){
					isTabIndex();
					CURRENT_TAB	=	5;
				}else if(tabId.equalsIgnoreCase("message")){	

					CURRENT_TAB = 3;
					
				/** 如果当前选项卡是me */
				}else if(tabId.equalsIgnoreCase("me")){	
					isTabMe();
					CURRENT_TAB = 4;
				}else{
					switch (CURRENT_TAB) {
					case 1:
						isTabHome();
						break;
					case 2:
						isTabWall();
						break;

					case 4:
						isTabMe();
						break;
					case 5:
						isTabIndex();
						break;
//					default:
//						isTabHome();
//						break;
					}		
					
				}
					ft.commit();	
			}
        	
        };
        tabHost.setOnTabChangedListener(tabChangeListener);
        /**  设置初始化界面  */
        int tag		=getIntent().getIntExtra("tag", 0);

        initTab();

        tabHost.setCurrentTab(0);
        tabHost.setCurrentTab(tag);
    }
    
    //判断当前
    public void isTabHome(){

    }
    
    public void isTabWall(){

    }
    
    public void isTabMe(){
    	

    }
    public void isTabIndex(){
    	if(index_Fragment==null){
			ft.add(R.id.realtabcontent,new Index_Fragment(), "index");						
		}else{
//			ft.attach(index_Fragment);	
			ft.show(index_Fragment);
		}
    }
    /**
     * 找到Tabhost布局
     */
    public void findTabView(){
    	
    	 tabHost = (TabHost) findViewById(android.R.id.tabhost);
         tabWidget = (TabWidget) findViewById(android.R.id.tabs);
         LinearLayout layout = (LinearLayout)tabHost.getChildAt(0);
         TabWidget tw = (TabWidget)layout.getChildAt(1);
         
         tabIndicator1 = (LinearLayout) LayoutInflater.from(this)
         		.inflate(R.layout.hotel_tab_indicator, tw, false);
         TextView tvTab1 = (TextView)tabIndicator1.getChildAt(1);
         ImageView ivTab1 = (ImageView)tabIndicator1.getChildAt(0);
         ivTab1.setBackgroundResource(R.drawable.hotel_filter_recommends_normal);
         tvTab1.setText("距离排序");
         
         tabIndicator2 = (LinearLayout) LayoutInflater.from(this)
         		.inflate(R.layout.hotel_tab_indicator, tw, false);
         TextView tvTab2 = (TextView)tabIndicator2.getChildAt(1);
         ImageView ivTab2 = (ImageView)tabIndicator2.getChildAt(0);

         tvTab2.setText("赛选价格");
         
         tabIndicator4 = (LinearLayout) LayoutInflater.from(this)
         		.inflate(R.layout.hotel_tab_indicator, tw, false);
         TextView tvTab4 = (TextView)tabIndicator4.getChildAt(1);
         ImageView ivTab4 = (ImageView)tabIndicator4.getChildAt(0);

         tvTab4.setText("赛选星级");
         
         tabIndicator5 = (LinearLayout) LayoutInflater.from(this)
         		.inflate(R.layout.hotel_tab_indicator, tw, false);
         TextView tvTab5 = (TextView)tabIndicator5.getChildAt(1);
         ImageView ivTab5 = (ImageView)tabIndicator5.getChildAt(0);
         ivTab5.setBackgroundResource(R.drawable.hotel_filter_distance_normal);
         tvTab5.setText("10KM内");
    }
    
    /** 
     * 初始化选项卡
     * 
     * */
    public void initTab(){
    	
        TabHost.TabSpec tSpecHome = tabHost.newTabSpec("home");
        tSpecHome.setIndicator(tabIndicator1);        
        tSpecHome.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecHome);
        
        TabHost.TabSpec tSpecWall = tabHost.newTabSpec("wall");
        tSpecWall.setIndicator(tabIndicator2);        
        tSpecWall.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecWall);
        
        TabHost.TabSpec tSpecMessage = tabHost.newTabSpec("message");
        tSpecMessage.setIndicator(tabIndicator4);      
        tSpecMessage.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecMessage);
        
        TabHost.TabSpec tSpecMe = tabHost.newTabSpec("me");
        tSpecMe.setIndicator(tabIndicator5);        
        tSpecMe.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecMe);
        
    }
    
}
